<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
 
        li {
            list-style: none;
        }
 
        a {
            text-decoration: none;
        }
 
        .nav {
            margin: 100px;
            border-top: 2px solid #ff8500;
        }
 
        .nav>li {
            position: relative;
            float: left;
            width: 150px;
            height: 41px;
            text-align: center;
        }
 
        .nav li a {
            display: block;
            width: 100%;
            height: 100%;
            line-height: 41px;
            color: #333;
        }
 
        /* .nav>li:hover {
            background-color: #eee;
        }
 
        .nav>li>a:hover {
            background-color: #eee;
            color: #ff8400;
        } */
 
        .nav ul {
            /* 隐藏 */
            display: none;
            position: absolute;
            top: 41px;
            left: 0;
            width: 100%;
            border-left: 1px solid #FECC5B;
            border-right: 1px solid #FECC5B;
        }
 
        .nav ul li {
            border-bottom: 1px solid #fecc5b;
        }
 
        .nav ul li a:hover {
            background-color: #fff5da;
        }
    </style>
</head>
 
<body>
    <ul class="nav">
        <li>
            <a href="#">微博</a>
            <ul>
                <li><a href="#">私信</a></li>
                <li><a href="#">评论</a></li>
                <li><a href="#">@我</a></li>
            </ul>
        </li>
        <li>
            <a href="#">博客</a>
            <ul>
                <li><a href="#">博客评论</a></li>
                <li><a href="#">未读提醒</a></li>
            </ul>
        </li>
        <li>
            <a href="#">邮箱</a>
            <ul>
                <li><a href="#">免费邮箱</a></li>
                <li><a href="#">VIP邮箱</a></li>
                <li><a href="#">企业邮箱</a></li>
                <li><a href="#">新浪邮箱客户端</a></li>
            </ul>
        </li>
        <li>
            <a href="#">网站导航</a>
            <ul>
                <li><a href="#">百度</a></li>
                <li><a href="#">CSDN</a></li>
                <li><a href="#">Github</a></li>
                <li><a href="#">MDN</a></li>
            </ul>
        </li>
    </ul>
 
    <script>
        // 1.导航栏里面的li都要有鼠标经过效果，所以需要循环触发鼠标事件
        // 2.核心原理：当鼠标经过li里面的第二个孩子 ul显示，当鼠标离开，则ul隐藏
 
        // 获取元素
        var nav = document.querySelector('.nav');
        var lis = nav.children;   // 得到4个小li
        for (var i = 0; i < lis.length; i++) {
            lis[i].onmouseover = function () {
                this.children[1].style.display = 'block';
                // console.log(this.children[i]);
                this.style.background="#eee";
                this.style.color="#ff8400";
            }
            lis[i].onmouseout = function () {
                this.children[1].style.display = 'none';
                this.style.background="#fff";
                this.style.color="#000";
            }
        }
    </script>
</body>
</body>
</html>
